package wendyeq.iweb.blog.dao.impl;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import wendyeq.iweb.blog.dao.IArchiveDAO;
import wendyeq.iweb.blog.pojo.Archive;
import wendyeq.iweb.blog.util.JdoManager;
@Repository("archiveDAO")
public class ArchiveDAO implements IArchiveDAO {
	Logger logger = Logger.getLogger(ArchiveDAO.class);
	
	@Override
	public boolean saveOrUpdate(Archive entity) {
		// TODO Auto-generated method stub
		PersistenceManager manager = JdoManager.getSession();
		try{
			manager.makePersistent(entity);
		}catch(Exception e){
			logger.error(e);
			return false;
		}finally{
			manager.close();
		}
		return true;
	}

	@Override
	public boolean delete(String id) {
		// TODO Auto-generated method stub
		PersistenceManager manager = JdoManager.getSession();
		try{
			Archive arch = manager.getObjectById(Archive.class,id);
			manager.deletePersistent(arch);
		}catch(Exception e){
			logger.error(e);
			return false;
		}finally{
			manager.close();
		}
		return true;
	}

	@Override
	public Archive findById(String id) {
		// TODO Auto-generated method stub
		Archive arch = null;
		PersistenceManager manager = JdoManager.getSession();
		try{
			arch = manager.getObjectById(Archive.class,id);
		}catch(Exception e){
			logger.error(e);
		}finally{
			manager.close();
		}
		return arch;
	}

	@Override
	public List<Archive> findByPage(int min, int max) {
		List<Archive> archives = null;
		PersistenceManager manager = JdoManager.getSession();
		try {
			Query query = manager.newQuery(Archive.class);
			query.setOrdering("createdTime DESC");
			query.setRange(min,max);
			archives = (List<Archive>) query.execute();
			logger.warn("archives-size: " + archives.size());
		} catch (Exception e) {
			logger.error(e);
		} 
		return archives;
	}
	@Override
	public Archive findByYM(String y,String m){
		List<Archive> archives = null;
		PersistenceManager manager = JdoManager.getSession();
		try {
			Query query = manager.newQuery(Archive.class);
			query.setFilter("year == y && month == m");
			query.declareParameters("String y,String m");
			archives = (List<Archive>) query.execute(y,m);
			logger.warn("archives-size: " + archives.size());
		} catch (Exception e) {
			logger.error(e);
		} 
		if(archives !=null&&archives.size()>0){
			return archives.get(0);
		}else{
			return null;
		}
	}
	
	@Override
	public List<Archive> findAll() {
		// TODO Auto-generated method stub
		List<Archive> archives = null;
		PersistenceManager manager = JdoManager.getSession();
		try {
			Query query = manager.newQuery(Archive.class);
			query.setOrdering("createdTime DESC");
			archives = (List<Archive>) query.execute();
			logger.warn("archives-size: " + archives.size());
		} catch (Exception e) {
			logger.error(e);
		} 
		return archives;
	}

}
